/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreCardTemplateAll;
import com.chlitina.store.modules.base.entity.StoreCardTemplateDuration;
import com.chlitina.store.modules.base.service.StoreCardTemplateAllService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 综合充值卡模板Controller
 * @author liujiang
 * @version 2013-11-02
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/storeCardTemplateAll")
public class StoreCardTemplateAllController extends BaseController {

	@Autowired
	private StoreCardTemplateAllService storeCardTemplateAllService;
	
	@ModelAttribute
	public StoreCardTemplateAll get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeCardTemplateAllService.get(id);
		}else{
			return new StoreCardTemplateAll();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(required=false)String currentSelectId, StoreCardTemplateAll storeCardTemplateAll, HttpServletRequest request, HttpServletResponse response, Model model) {
		String searchStatus = request.getParameter("searchStatus");
		Page<StoreCardTemplateAll> page = storeCardTemplateAllService.find(new Page<StoreCardTemplateAll>(request, response), storeCardTemplateAll, searchStatus); 
		model.addAttribute("searchStatus", searchStatus);
		model.addAttribute("currentSelectId", currentSelectId);
		model.addAttribute("page", page);
		return "modules/base/storeCardTemplateAllList";
	}

	@RequestMapping(value = "form")
	public String form(StoreCardTemplateAll storeCardTemplateAll, Model model) {
		model.addAttribute("storeCardTemplateAll", storeCardTemplateAll);
		return "modules/base/storeCardTemplateAllForm";
	}

	@RequestMapping(value = "save")
	public String save(String pageNo, StoreCardTemplateAll storeCardTemplateAll, Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		storeCardTemplateAll.setStore(store);
		storeCardTemplateAll.setCare_discount(storeCardTemplateAll.getCare_discount()/100);
		storeCardTemplateAll.setProduct_discount(storeCardTemplateAll.getProduct_discount()/100);
		if (!beanValidator(model, storeCardTemplateAll)){
			return form(storeCardTemplateAll, model);
		}
		storeCardTemplateAllService.save(storeCardTemplateAll);
		addMessage(redirectAttributes, "保存综合充值卡模板'" + storeCardTemplateAll.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeCardTemplateAll/?pageNo=" + pageNo +"&currentSelectId=" +storeCardTemplateAll.getId();
	}
	
	@RequestMapping(value = "delete")
	public String delete(StoreCardTemplateAll storeCardTemplateAll, RedirectAttributes redirectAttributes) {
		int status = storeCardTemplateAll.getStatus();
		if (status == StoreCardTemplateAll.FLAG_ENABLE) {
			storeCardTemplateAll.setStatus(StoreCardTemplateAll.FLAG_DISABLE);
		} else {
			storeCardTemplateAll.setStatus(StoreCardTemplateAll.FLAG_ENABLE);
		}
		storeCardTemplateAllService.save(storeCardTemplateAll);
		addMessage(redirectAttributes, "综合充值卡模板状态更改成功！");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeCardTemplateAll/";
	}

}
